﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;

namespace LimpaGaragem.DAO.Product
{
    public class Comment
    {
        /// <summary>
        /// Return list of comment
        /// </summary>
        /// <returns></returns>
        public List<LimpaGaragem.Model.Product.Comment> GetListOfComment(int IDProduct)
        {
            try
            {
                Framework.DataAccess objDataAccess = new Framework.DataAccess();

                //PARAMETERS
                List<SqlParameter> objListOfParameters = new List<SqlParameter>();
                objListOfParameters.Add(new SqlParameter("@ID_PRODUCT", IDProduct));
                DataTable objDataTable = objDataAccess.ReturnDataTable(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString, Framework.DataAccess.QueryType.Procedure, "PR013_ListComment", 0, objListOfParameters);

                List<LimpaGaragem.Model.Product.Comment> objListOfComment = new List<LimpaGaragem.Model.Product.Comment>();
                foreach (DataRow objDataRow in objDataTable.Rows)
                {
                    LimpaGaragem.Model.Product.Comment objComment = new LimpaGaragem.Model.Product.Comment();
                    objComment.IDProduct = IDProduct;
                    objComment.ID = Convert.ToInt32(objDataRow["ID_COMMENT"]);
                    if(objDataRow["ID_COMMENT_PARENT"] != DBNull.Value)
                        objComment.IDParent = Convert.ToInt32(objDataRow["ID_COMMENT_PARENT"]);
                    objComment.IDUser = Convert.ToInt32(objDataRow["ID_USER"]);
                    objComment.Content = objDataRow["DS_CONTENT"].ToString();
                    objComment.DateTimeInserted = Convert.ToDateTime(objDataRow["DT_INC"]);
                    objListOfComment.Add(objComment);
                }
                return objListOfComment;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Insert new comment
        /// </summary>
        public int InsertComment(int? IDCommentParent,
                                    int IDProduct,
                                    int IDUser,
                                    string Content)
        {
            try
            {
                Framework.DataAccess objDataAccess = new Framework.DataAccess();

                //PARAMETERS
                List<SqlParameter> objListOfParameters = new List<SqlParameter>();
                if (IDCommentParent.HasValue)
                    objListOfParameters.Add(new SqlParameter("@ID_COMMENT_PARENT", IDCommentParent));
                else
                    objListOfParameters.Add(new SqlParameter("@ID_COMMENT_PARENT", DBNull.Value));
                objListOfParameters.Add(new SqlParameter("@ID_PRODUCT", IDProduct));
                objListOfParameters.Add(new SqlParameter("@ID_USER", IDUser));
                objListOfParameters.Add(new SqlParameter("@DS_CONTENT", Content));

                //EXECUTE
                return objDataAccess.ExecuteQueryAndReturnValue(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString, Framework.DataAccess.QueryType.Procedure, "PR013_InsertComment", 0, objListOfParameters);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Delete comment
        /// </summary>
        public void DeleteComment(int IDComment)
        {
            try
            {
                Framework.DataAccess objDataAccess = new Framework.DataAccess();

                //PARAMETERS
                List<SqlParameter> objListOfParameters = new List<SqlParameter>();
                objListOfParameters.Add(new SqlParameter("@ID_COMMENT", IDComment));

                //EXECUTE
                objDataAccess.ExecuteQuery(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString, Framework.DataAccess.QueryType.Procedure, "PR013_DeleteComment", 0, objListOfParameters);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
